package com.project.weather.service.impl;

import com.project.weather.enums.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.project.weather.dao.OperLogMapper;
import com.project.weather.pojo.OperLogBean;
import com.project.weather.service.OperLogService;
import com.project.weather.common.PageBean;
import com.project.weather.common.AResultCode;
import com.project.weather.common.ResultMap;
import com.project.weather.utils.StringUtil;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *<p>Title:OperLogServiceImpl.java</p>
 *<p>Description:操作日志记录ServiceImpl</p>
 *@author xxx
 *@version 1.0
 *@Automatically generate by Coder in 2021-12-21 16:08
 */
@Service
public class OperLogServiceImpl implements OperLogService{
	private static final Logger log = LoggerFactory.getLogger(OperLogServiceImpl.class);
	@Autowired
	private OperLogMapper operLogMapper;
	
	@Override
	public ResultMap<AResultCode, OperLogBean> insert(OperLogBean bean){
		ResultMap<AResultCode, OperLogBean> result = new ResultMap<AResultCode, OperLogBean>();
		if(StringUtil.isNull(bean)){
			result.setCode(AResultCode.IS_NULL);
		} else {
//			if(StringUtil.isNull(bean.getOper_id())){
//				bean.setOper_id(StringUtil.getUUID());
//			}
			operLogMapper.insertSelective(bean);	
			result.setCode(AResultCode.OK);
			result.setResult(bean);
		}
		return result;
	}

	
	@Override
	public ResultMap<AResultCode, OperLogBean> update(OperLogBean bean){
		ResultMap<AResultCode, OperLogBean> result = new ResultMap<AResultCode, OperLogBean>();

		if(StringUtil.isNull(bean)){
			result.setCode(AResultCode.IS_NULL);
		} else if(StringUtil.isNull(bean.getOper_id())){
			result.setCode(AResultCode.MISS_ID);
		} else {
			operLogMapper.updateSelective(bean);	
			result.setCode(AResultCode.OK);
			result.setResult(bean);
		}

		return result;
	}

	
	@Override
	public ResultMap<AResultCode, OperLogBean> delete(String oper_id){
		ResultMap<AResultCode, OperLogBean> result = new ResultMap<AResultCode, OperLogBean>();

		if(StringUtil.isNull(oper_id)){
			result.setCode(AResultCode.MISS_ID);
		} else {
			String[] ids = oper_id.split(",");
			operLogMapper.delete(ids);
			result.setCode(AResultCode.OK);
		}

		return result;
	}

	
	@Override
	public ResultMap<AResultCode, PageBean<OperLogBean>> pageList(PageBean<OperLogBean> pb){
		ResultMap<AResultCode, PageBean<OperLogBean>> result = new ResultMap<AResultCode, PageBean<OperLogBean>>();

		List<OperLogBean> operLogBeans = operLogMapper.pageList(pb);

		for(OperLogBean operLogBean : operLogBeans){
			Integer business_type = operLogBean.getBusiness_type();

			String business_type_name = "";
			switch(business_type){
				case 0:
					business_type_name="其它";
					break;
				case 1:
					business_type_name="新增";
					break;
				case 2:
					business_type_name="修改";
					break;
				case 3:
					business_type_name="删除";
					break;
				case 4:
					business_type_name="授权";
					break;
				case 5:
					business_type_name="解除授权";
					break;
				case 6:
					business_type_name="导出";
					break;
				case 7:
					business_type_name="导入";
					break;
				case 8:
					business_type_name="查询";
					break;
				case 9:
					business_type_name="登录";
					break;
				case 10:
					business_type_name="注销";
					break;
				case 11:
					business_type_name="注册";
					break;
				case 12:
					business_type_name="禁用用户";
					break;
				case 13:
					business_type_name="解除禁用用户";
					break;
				case 14:
					business_type_name="查询当天天气";
					break;
				case 15:
					business_type_name="查询未来7天天气";
					break;
			}

			operLogBean.setBusiness_type_name(business_type_name);
		}

		pb.setData(operLogBeans);
		pb.clear();

		result.setCode(AResultCode.OK);
		result.setResult(pb);

		return result;
	}

	
	@Override
	public ResultMap<AResultCode, List<OperLogBean>> list(OperLogBean bean){
		ResultMap<AResultCode, List<OperLogBean>> result = new ResultMap<AResultCode, List<OperLogBean>>();

		List<OperLogBean> list = operLogMapper.list(bean);
		result.setCode(AResultCode.OK);
		result.setResult(list);

		return result;
	}

	
	@Override
	public ResultMap<AResultCode, OperLogBean> get(String oper_id){
		ResultMap<AResultCode, OperLogBean> result = new ResultMap<AResultCode, OperLogBean>();
		if(StringUtil.isNull(oper_id)){
			result.setCode(AResultCode.MISS_ID);
		} else {
			OperLogBean bean = operLogMapper.get(oper_id);
			if(bean != null) {
				result.setCode(AResultCode.OK);
				result.setResult(bean);
			} else {
				result.setCode(AResultCode.UNEXIST);
	 		}
		}
		return result;
	}

	
}